package com.example.tests;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ContactCreationTests extends TestBase {

	Random rnd = new Random();

	@DataProvider(name = "randomContacts")
	public Iterator<Object[]> generateRandomContacts() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			ContactObject contact = new ContactObject()
					.setFirstName("Enter_first_name" + rnd.nextInt())
					.setLastName("Enter_last_name" + rnd.nextInt())
					.setAddressFirst("Enter_address" + rnd.nextInt())
					.setHomePhone("Home phone" + rnd.nextInt())
					.setMobilePhone("mobile phone" + rnd.nextInt())
					.setWorkPhone("work phone" + rnd.nextInt())
					.setEmailFirst("email1@fnwiouefn.nvuw" + rnd.nextInt())
					.setEmailSecond("ojnvfwefno32@fvnwerf.vnui" + rnd.nextInt())
					.setBirthYear("19" + rnd.nextInt(99))
					.setAddressSecond("secondary adress here" + rnd.nextInt())
					.setPhoneSecond("home adressssss" + rnd.nextInt())
					.setBirthDate("15")
					.setBirthMonth("July");
			list.add(new Object[] { contact });
		}
		return list.iterator();
	}

	@Test(dataProvider = "randomContacts")
	public void testContactCreationWithAllData(ContactObject validContact)
			throws Exception {
		Set<ContactObject> oldList = app.getContactHelper().getContacts();
		// action
		app.getContactHelper().createContact(validContact);
		// validate
		Set<ContactObject> newList = app.getContactHelper().getContacts();
		verifyContactAdded(oldList, validContact, newList);
	}
	
	private void verifyContactAdded(Set<ContactObject> oldList,
			ContactObject someContact, Set<ContactObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size() + 1));
		someContact.setId("###");
		oldList.add(someContact);
		someContact.setId(null);
		assertThat(newList, equalTo(oldList));
	}

	@Test
	public void testContactCreationWithoutData() throws Exception {
		ContactObject invalidContact = new ContactObject()
				.setFirstName("")
				.setLastName("")
				.setAddressFirst("")
				.setHomePhone("")
				.setMobilePhone("")
				.setWorkPhone("")
				.setEmailFirst("")
				.setEmailSecond("")
				.setBirthYear("")
				.setAddressSecond("")
				.setPhoneSecond("")
				.setBirthDate("-")
				.setBirthMonth("-");
		app.getContactHelper().createContact(invalidContact);
		// validate
	}

}
